package ru.gerunn.silent.test.manyreaderstoonestream;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import ru.gerunn.silent.exception.SilentReadMessageException;
import ru.gerunn.silent.io.ConsoleReader;
import ru.gerunn.silent.io.Reader;
import ru.gerunn.silent.message.Message;

public class Main {

	private static class RR implements Callable<Object> {

		private final Reader reader;
		private final String name;

		public RR(final Reader reader, String name) {
			this.reader = reader;
			this.name = name;
		}

		public Object call() throws SilentReadMessageException {
			Message message;
			while ((message = reader.read()) != null) {
				System.out.println(name + "\t" + message);
			}
			return null;
		}
	}

	public static void main(String[] args) {
		ConsoleReader cr1 = new ConsoleReader();
		ConsoleReader cr2 = new ConsoleReader();

		RR rr1 = new Main.RR(cr1, "cr1");
		RR rr2 = new Main.RR(cr2, "cr2");

		ExecutorService pool = Executors.newFixedThreadPool(2);
		Future<Object> f1 = pool.submit(rr1);
		Future<Object> f2 = pool.submit(rr2);

		try {
			f1.get();
			f2.get();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}

	}

}
